package com.toonly.loganalysis;

import toonly.wrapper.SW;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by caoyouxin on 1/14/16.
 */
public class Main {

    public static void main(String[] args) throws FileNotFoundException, ParseException {
        FileReader fileReader = new FileReader("ycarapi-2016-01-09-info.log");
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        Map<String, List<String>> map = new ConcurrentHashMap<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date start = simpleDateFormat.parse("2016-01-09 15:30:00");
        Date end = simpleDateFormat.parse("2016-01-09 15:50:00");
        final SW<Date> last = new SW<>(start);

        bufferedReader.lines().forEachOrdered((line) -> {
            String $1 = line.replaceAll("(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}).*?(\\[.*?\\]).*", "$1");
            String $2 = line.replaceAll("(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}).*?(\\[.*?\\]).*", "$2");
//            System.out.println($1 + $2);

            try {
                Date date = simpleDateFormat.parse($1);
                if (date.after(end) || date.before(start)) {
                    return;
                }
                if (date.after(last.val())) {
                    last.val(date);
                }
            } catch (ParseException ignored) {
            }

            List<String> stringList = map.get($2);

            if (null == stringList) {
                stringList = new ArrayList<>();
                map.put($2, stringList);
            }

            stringList.add(line);
        });

        int count = 0;
        int[] counts = new int[5];
        for (String key : map.keySet()) {
            List<String> strings = map.get(key);

            for (int i = 7; i > 0; i--) {
                if (strings.size() >= i) {
                    System.out.println(strings.get(strings.size() - i));
                }
                if (i == 1) {
                    String $1 = strings.get(strings.size() - i).replaceAll("(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}).*?(\\[.*?\\]).*", "$1");
                    Date date = simpleDateFormat.parse($1);
                    long len = last.val().getTime() - date.getTime();
                    for (int j = 0; j < counts.length; j++) {
                        if (len > (j + 1) * 60 * 1000) {
                            counts[j]++;
                        }
                    }
                }
            }

            if (strings.size() > 2) {
                count++;
            }

            System.out.println();
        }
        System.out.println();
        System.out.println("开始计时:" + simpleDateFormat.format(start));
        System.out.println("结束计时:" + simpleDateFormat.format(end));
        System.out.println();
        System.out.println("startStop线程:");
        List<String> strings = map.get("[localhost-startStop-1]");
        if (null != strings) {
            strings.forEach(System.out::println);
        }
        System.out.println();
        System.out.println("线程数:" + map.keySet().size());
        System.out.println("旧线程数:" + count);
        System.out.println("阻塞线程数:" + Arrays.toString(counts));
        System.out.println("程序停止时间:" + simpleDateFormat.format(last.val()));
    }

}
